from TsmApi import TsmApi
from collections import namedtuple

dsmDate            = namedtuple("dsmDate", "year, month, day, hour, minute, second")
dsStruct64         = namedtuple("dsStruct64", "hi, lo")
qryRespFSData      = namedtuple("qryRespFSData", "fsName, fsType, occupancy, capacity, backStartDate, backCompleteDate")
dsmObjName         = namedtuple("dsmObjName", "fs, hl, ll, objType")
dsStruct160        = namedtuple("dsStruct160", "top, hi_hi, hi_lo, lo_hi, lo_lo")
qryRespArchiveData = namedtuple("qryRespArchiveData", "objName, copyGroup, mcName, owner, objId, reserverd, mediaClass,\
				 insDate, expDate, descr, objInfolen, objInfo, restoreOrderExt, sizeEstimate, compressType")

class dsStruct160(object):
    def __init__(self, top=0, hi_hi=0, hi_lo=0, lo_hi=0, lo_lo=0):
	self.top   = top
	self.hi_hi = hi_hi
	self.hi_lo = hi_lo
	self.lo_hi = lo_hi
	self.lo_lo = lo_lo




class pyTsm(TsmApi):
    pass
    
    def dsmGetNextQObj(self):
	if self.inQuery:
	    qryResp =  self.tsmGetNextQObj()
	    if qryResp:
		return qryRespFSData(qryResp[0], \
				     qryResp[1], \
				     dsStruct64(qryResp[2][0], qryResp[2][1]), \
				     dsStruct64(qryResp[3][0], qryResp[3][1]), \
				     dsmDate(qryResp[4][0],qryResp[4][1],qryResp[4][2],qryResp[4][3],qryResp[4][4],qryResp[4][5]), \
				     dsmDate(qryResp[5][0],qryResp[5][1],qryResp[5][2],qryResp[5][3],qryResp[5][4],qryResp[5][5]))
				    
	
	return None
    
tsm = pyTsm("/opt/tivoli/tsm/client/ba/bin/dsm.opt")

tsm.tsmInitEx("HRARCH1P", None, "Hrarch1p1")

print tsm.dsmHandle

tsm.tsmBeginQueryArchive(("/2012", "/*", "/PG-25051-7-BONESYR2-2AKY01-THETITANONTHETRACKS-R4FJ.mxf", 1), None)

print tsm.tsmRCMsg()
t = tsm.tsmGetNextQObj()
print t
objid = t[4]

print objid

tsm.tsmEndQuery()

print tsm.dsmHandle

f = open("ARDAYR2-DPXN21-EPISODIO21-6KO2.mxf", "wb");

tsm.tsmBeginGetData(True,1,[objid])
print tsm.tsmRCMsg()

buff = tsm.tsmGetObj(objid)
print tsm.tsmRCMsg()
while buff:
    f.write(buff)
    buff = tsm.tsmGetData()
    
f.close()


tsm.tsmTerminate()